package com.example.demo14;

import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import com.example.demo14.javabean.Book;

import org.litepal.LitePal;
import org.litepal.tablemanager.Connector;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button createDB=findViewById(R.id.create_db);
        createDB.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.N)
            @Override
            public void onClick(View v) {
                Connector.getDatabase();
                Book book=new Book();
                book.setAuthor("郭霖");
                book.setBookName("第一行代码（第3版）");
                book.setPages(580);
                book.setPrice(73.5);
                //添加数据
                book.save();
                book.setPrice(140);
                book.save();
                book.setPrice(21);
                //更新数据
                book.updateAll("id = ? or id = ?", "1", "2");
                //删除数据
                LitePal.deleteAll(Book.class,"id = ? or id = ?", "1", "2");
                LitePal.delete(Book.class,3);
                book.delete();
                //查询数据
                List<Book> books = LitePal.findAll(Book.class);
                books.forEach((Book book1)-> System.out.println(book1.toString()));
            }
        });
    }
}
